package com.lei.zjs.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lei.zjs.service.ebi.RegionService;
import com.lei.zjs.utils.PinYin4jUtils;
import com.lei.zjs.vo.PageBean;
import com.lei.zjs.vo.Region;
import com.lei.zjs.web.action.base.BaseAction;

/**
 * 区域管理Action
 * 
 * @author Spinacia
 * 
 */
@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region> {

	@Resource
	private RegionService regionService;

	private File myFile;
	private String myFileFileName;

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	/**
	 * 导入区域文件信息
	 * 
	 * @return
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public String importExcel() throws FileNotFoundException, IOException {
		// 设置标示
		String flag = "1";
		Workbook workbook = null;
		try {
			if (myFileFileName.endsWith(".xlsx")) {
				workbook = new XSSFWorkbook(new FileInputStream(myFile));
			} else {
				workbook = new HSSFWorkbook(new FileInputStream(myFile));
			}
			Sheet sheet = workbook.getSheetAt(0);

			List<Region> list = new ArrayList<Region>();
			for (Row row : sheet) {
				int rowNum = row.getRowNum();
				// 不要第一行,都是头标题
				if (rowNum == 0) {
					continue;
				}

				String id = row.getCell(0).getStringCellValue();
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				String postcode = row.getCell(4).getStringCellValue();

				String shortcode = province + city + district;
				String[] shortcodes = PinYin4jUtils.getHeadByString(shortcode);
				shortcode = StringUtils.join(shortcodes);

				String[] citycodes = PinYin4jUtils.stringToPinyin(city);
				String citycode = StringUtils.join(citycodes);
				Region region = new Region(id, province, city, district,
						postcode, shortcode, citycode, null);

				list.add(region);
			}
			regionService.importExcel(list);
		} catch (Exception e) {
			flag = "0";
			e.printStackTrace();
		}

		ServletActionContext.getResponse().setContentType(
				"text/html;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(flag);
		return NONE;
	}

	private Map map = new HashMap();

	public Map getMap() {
		return map;
	}

	/**
	 * 分页查询显示所有区域信息
	 */
	public String pageQuery() {
		regionService.pageQuery(pageBean);

		map.put("total", pageBean.getTotal());
		map.put("rows", pageBean.getRows());

		return "pageQuery";
	}

	/**
	 * 保存区域信息
	 */
	public String save() {
		regionService.save(model);
		return "list";
	}

	private Region region = new Region();

	public Region getRegion() {
		return region;
	}

	/**
	 * 查询某个区域信息
	 */
	public String getOneRegion() {
		region = regionService.getOneRegion(model.getId());
		return "editAjax";
	}

	/**
	 * 修改区域信息
	 */
	public String update() {
		regionService.update(model);
		return "list";
	}

	private List<Region> list;

	public List<Region> getList() {
		return list;
	}

	/**
	 * 加载combobox的区域数据
	 */
	public String loadListByAjax() {
		list = regionService.findAll();
		return "regionJson";
	}
}
